local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")


local FirstgameLayer = class("FirstgameLayer",function(scene)
    local alipay = display.newLayer(cc.c4b(0, 0, 0, 125))
	return alipay
end)

function FirstgameLayer:ctor(scene)
    ExternalFun.registerNodeEvent(self)
    self._scene = scene

    self:loadCSB()
  --  self:init()

	self:setContentSize(yl.WIDTH,yl.HEIGHT)
	


end
function FirstgameLayer:onExit()

end
-- 进入场景而且过渡动画结束时候触发。
function FirstgameLayer:onEnterTransitionFinish()
    return self
end

-- 退出场景而且开始过渡动画时候触发。
function FirstgameLayer:onExitTransitionStart()
	self:unregisterScriptTouchHandler()
    return self
end

function FirstgameLayer:loadCSB()
    local csbnode = ExternalFun.loadCSB("popView/xinshouLayer.csb",self)
    self.bg = csbnode:getChildByName("bg")
	self.orderLayer =  csbnode:getChildByName("orderLayer")
	self.orderLayer:setVisible(false)
	self.bg:setVisible(true)
    local bg = self.bg

    self.btn_close = bg:getChildByName("closebt")
    self.btn_sure = bg:getChildByName("go_btn")


    local btnCallback = function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setOpacity(255)
			self:onButtonClicked(ref:getTag(),ref)
		elseif type == ccui.TouchEventType.began then
			ref:setOpacity(180)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setOpacity(255)
		end
    end
    
    self.btn_close:addTouchEventListener(btnCallback)
    self.btn_sure:addTouchEventListener(btnCallback)
	--区域外取消显示
	local  onQueryExitTouch = function(eventType, x, y)

		if eventType == "ended" then
			local rect = anniu:getBoundingBox()
        	if cc.rectContainsPoint(rect,cc.p(x,y)) == true then
        		print("sdf")
    		end
		end
    	return true
    end
	self:setTouchEnabled(true)
	self:registerScriptTouchHandler(onQueryExitTouch)
	
end

function FirstgameLayer:init()
	local size = self.tf_alipaycount:getContentSize()
	local posx,posy = self.tf_alipaycount:getPosition()
	self.tf_alipaycount:removeFromParent()
	self.tf_alipaycount = nil

	self.tf_alipaycount =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(27)
	:setPlaceholderFontSize(27)
	:setInputMode(cc.EDITBOX_INPUT_MODE_EMAILADDR)
	:setMaxLength(30)
	:setPlaceHolder("输入支付宝账号")
	 self.tf_alipaycount:registerScriptEditBoxHandler(function(event,editbox)
        if event == "return" then
           local edittext = editbox:getText()
			edittext = tostring(edittext)
			if  self:CheckChinese(edittext) then
				showToast(self._scene,"含有非法字符",2)
				editbox:setText("")
				return
			end
			editbox:setText(edittext)
			if string.find(edittext,'[a-zA-Z]')  then
				if not string.find(edittext,'@') or not string.find(edittext,'%.') then
					
					self.nopass = true
				else
					self.nopass = false
				end
			elseif string.find(edittext,'[a-zA-Z0-9]') == nil then
				self.nopass = true
			else
				self.nopass = false
			end
        end
    end)
	self.bg:addChild(self.tf_alipaycount)
end

function FirstgameLayer:onButtonClicked(tag, ref)
	if ref == self.btn_close then
		self:removeFromParent()
	elseif ref == self.btn_sure then
		self.orderLayer:setVisible(true)
		self.bg:setVisible(false)
	end
end

return FirstgameLayer